﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;

namespace Framework.AspNetCore.UnifyResult
{
	/// <summary>
	/// 常量、公共方法配置类
	/// </summary>
	internal static class Penetrates
	{
		/// <summary>
		/// 构造函数
		/// </summary>
		static Penetrates()
		{
			IsApiControllerCached = new ConcurrentDictionary<Type, bool>();
		}

		/// <summary>
		/// <see cref="IsApiController(Type)"/> 缓存集合
		/// </summary>
		private static readonly ConcurrentDictionary<Type, bool> IsApiControllerCached;

		/// <summary>
		/// 是否是Api控制器
		/// </summary>
		/// <param name="type">type</param>
		/// <returns></returns>
		internal static bool IsApiController(Type type)
		{
			return IsApiControllerCached.GetOrAdd(type, Function);

			// 本地静态方法
			static bool Function(Type type)
			{
				// 排除 OData 控制器
				if (type.Assembly.GetName().Name.StartsWith("Microsoft.AspNetCore.OData")) return false;

				// 不能是非公开、基元类型、值类型、抽象类、接口、泛型类
				if (!type.IsPublic || type.IsPrimitive || type.IsValueType || type.IsAbstract || type.IsInterface || type.IsGenericType) return false;

				// 继承 Controller 或 继承 ControllerBase特性 ApplicationService
				if ((!typeof(Controller).IsAssignableFrom(type) && typeof(ControllerBase).IsAssignableFrom(type)) || typeof(ApplicationService).IsAssignableFrom(type)) return true;

				return false;
			}
		}
	}
}
